import fetch from "node-fetch";
import puppeteer from './mypuppeteer.js'

import md5 from 'md5'

var groupId = 742781376 ;

export class stplus extends plugin {
  constructor () {
    super({
      name: '每日一题',
      dsc: '#每日一题',
      event: 'message',
      rule: [
        {
          reg: '^#每日一题$',
          fnc: 'dailyquestion'
        }
      ]
    })
	/** 定时任务 */
    this.task = {
      cron: "1 0 0 * * ?",
      name: '每日一题推送',
      fnc: () => this.sendToGroup()
    }

  }

  async sendToGroup() {
	var o = await this.findques() ;
	await Bot.pickGroup(groupId).sendMsg("每日零点自动推送\n"+o.msg);
	await Bot.pickGroup(groupId).sendMsg(o.img);	
  }

  async dailyquestion () {
	// console.log(this.e) ;
	if(this.e.group_id != groupId) {
		return ;
	}
	  
    var o = await this.findques() ;
	
	await this.reply(o.msg) ;
	await this.reply(o.img) ;
  }

  async findques() {
	
	let url = 'https://leetcode.cn/graphql/' ;
    let res = await fetch(url,{
		method: 'post',
		body: JSON.stringify({
			query: "\n    query questionOfToday {\n  todayRecord {\n    date\n    userStatus\n    question {\n      questionId\n      frontendQuestionId: questionFrontendId\n      difficulty\n      title\n      titleCn: translatedTitle\n      titleSlug\n      paidOnly: isPaidOnly\n      freqBar\n      isFavor\n      acRate\n      status\n      solutionNum\n      hasVideoSolution\n      topicTags {\n        name\n        nameTranslated: translatedName\n        id\n      }\n      extra {\n        topCompanyTags {\n          imgUrl\n          slug\n          numSubscribed\n        }\n      }\n    }\n    lastSubmission {\n      id\n    }\n  }\n}\n    " 
		}), 
		headers: {
			'Content-Type': 'application/json'
		}
	})
	.then(res => res.json());
	
	let questionTitleSlup = res.data.todayRecord[0].question.titleSlug ; 
	// questionTitleSlup = "minimum-moves-to-reach-target-with-rotations" ;
	
	let msg = "["+res.data.todayRecord[0].question.difficulty+"]"+res.data.todayRecord[0].question.titleCn+"\n https://leetcode.cn/problems/"+res.data.todayRecord[0].question.titleSlug+"/";
    let problem = await fetch(url,{
		method: 'post',
		body: JSON.stringify({
			operationName: "questionData" ,
			query: "query questionData($titleSlug: String!) {\n  question(titleSlug: $titleSlug) {\n    questionId\n    questionFrontendId\n    categoryTitle\n    boundTopicId\n    title\n    titleSlug\n    content\n    translatedTitle\n    translatedContent\n    isPaidOnly\n    difficulty\n    likes\n    dislikes\n    isLiked\n    similarQuestions\n    contributors {\n      username\n      profileUrl\n      avatarUrl\n      __typename\n    }\n    langToValidPlayground\n    topicTags {\n      name\n      slug\n      translatedName\n      __typename\n    }\n    companyTagStats\n    codeSnippets {\n      lang\n      langSlug\n      code\n      __typename\n    }\n    stats\n    hints\n    solution {\n      id\n      canSeeDetail\n      __typename\n    }\n    status\n    sampleTestCase\n    metaData\n    judgerAvailable\n    judgeType\n    mysqlSchemas\n    enableRunCode\n    envInfo\n    book {\n      id\n      bookName\n      pressName\n      source\n      shortDescription\n      fullDescription\n      bookImgUrl\n      pressImgUrl\n      productUrl\n      __typename\n    }\n    isSubscribed\n    isDailyQuestion\n    dailyRecordStatus\n    editorType\n    ugcQuestionId\n    style\n    exampleTestcases\n    jsonExampleTestcases\n    __typename\n  }\n}\n" ,
			variables: {
				titleSlug: questionTitleSlup
			}
		}), 
		headers: {
			'Content-Type': 'application/json'
		}
	})
	.then(problem => problem.json());
	
	let question = problem.data.question ;
	let questionTest = question.translatedContent ;
	
	questionTest = questionTest
		.split("&nbsp;").join(" ") ;
	
	let data = new Object ;
	questionTest = questionTest
		.split("\n").join("") 
		.split("\t").join("") ;
		
			
	data.questionTest = questionTest ;
	data.tplFile = process.cwd() + "/plugins/dailyquestion/question.html" ;
	let img = await puppeteer.screenshot('help', data) ;

	let o = new Object ;
	o.img = img ;
	o.msg = msg ;
	return o ;

  }
}
